﻿using Adnc.Application.Shared;
using Adnc.Application.Shared.Dtos;
using Adnc.WebApi.Shared;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using WYT.Usr.Application.Contracts.Dtos;
using WYT.Usr.Application.Contracts.Services;

namespace WYT.Usr.WebApi.Controllers
{
    /// <summary>
    /// 用户收货地址管理
    /// </summary>
    [Route("usr/[Controller]")]
    [ApiController]
    public class AddressController : AdncControllerBase
    {
        private readonly IUserContext _userContext;

        private readonly IAddressAppService _addressService;

        public AddressController(IUserContext userContext, IAddressAppService addressService)
        {
            _userContext = userContext;
            _addressService = addressService;
        }

        /// <summary>
        /// 添加用户收货地址
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("add")]
        public async Task<WYTActionResult<long>> AddUserHarvestAddress(UserAddressCreationDto input)
        {
            return Success(await _addressService.CreateAsync(_userContext.Id, input));
        }

        /// <summary>
        /// 修改用户收货地址
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("edit")]
        public async Task<WYTActionResult<bool>> UpdateUserHarvestAddress(UserAddressUpdationDto input)
        {
            return Success(await _addressService.UpdateAsync(_userContext.Id, input));
        }

        /// <summary>
        /// 删除用户收货地址
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<WYTActionResult<bool>> DeleteAsync([FromBody]SimpleInputDto<long> Input)
        {
            return Success(await _addressService.DeleteAsync(_userContext.Id, Input.Value));
        }

        /// <summary>
        /// 获取用户收货地址列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("list")]
        public async Task<WYTActionResult<List<UserAddressDto>>> GetlistAsync()
        {
            return Success(await _addressService.GetUserHarvestAddress(_userContext.Id));

        }

        #region

        /// <summary>
        /// 获取用户收货地址详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("_detail")]
        public async Task<ActionResult<UserAddressDto>> GetAsync([FromQuery] long Id)
        {
            return Result(await _addressService.SearchById(Id));
        }

        #endregion
    }
}
